package com.spring.cold.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CustomUserDetailsService userDetailsService;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                return charSequence.toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                return s.equals(charSequence.toString());
            }
        });
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                // 如果有允许匿名的url，填在下面
                .antMatchers("/customize/**","/assets/**","/lr/**","/second/**","/user/showRegister",
                        "/user/new","/layouts/error.html").permitAll()
                .anyRequest().authenticated()
                .and()
                // 设置登陆页
                .formLogin().loginPage("/showLogin")
                .loginProcessingUrl("/login")
                // 设置登陆成功页
                .defaultSuccessUrl("/home").permitAll()
                //设置登录得账户密码参数
                .usernameParameter("loginName")
                .passwordParameter("password")
                .and()
                .logout().permitAll().logoutSuccessHandler(logoutSuccessHandler())
                .and()
                .rememberMe()
                .and()
                //使用iframe嵌入网页需要配置为disable
                .headers().frameOptions().disable();

        // 关闭CSRF跨域
        http.csrf().disable();
    }


    @Bean
    public LogoutSuccessHandler logoutSuccessHandler(){
        return (httpServletRequest, httpServletResponse, authentication) -> {
            try {
                CustomizeUser user = (CustomizeUser) authentication.getPrincipal();
            } catch (Exception e) {
            }
            httpServletResponse.sendRedirect("/");
        };
    }
}
